<template>
  <div class="article-header" :class="{ 'with-cover': article.coverImage }">
    <!-- 封面图（可选） -->
    <div v-if="article.coverImage" class="cover-wrapper">
      <img :src="article.coverImage" :alt="article.title" class="cover-image" />
    </div>
    <div class="header-content">
      <h1 class="title">{{ article.title }}</h1>
      <div class="meta">
        <span class="category" v-if="article.category && article.category.name">
          <i class="ri-folder-3-line"></i>
          {{ article.category.name }}
        </span>
        <span class="read-time">
          <i class="ri-timer-line"></i>
          {{ estimateReadTime(article.content) }}
        </span>
        <span class="date">
          <i class="ri-calendar-line"></i>
          {{ timeAgo(article.publishDate) }}
        </span>
        <span class="views">
          <i class="ri-eye-line"></i>
          {{ article.views }}
        </span>
        <span class="likes">
          <i class="ri-thumb-up-line"></i>
          {{ article.likes }}
        </span>
        <span class="comments">
          <i class="ri-chat-3-line"></i>
          {{ commentsCount }}
        </span>
      </div>
      <div class="tags">
        <span v-for="tag in article.tags" :key="tag.id" class="tag">
          <i class="ri-price-tag-3-line"></i> {{ tag.name }}
        </span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { timeAgo } from '@/utils/timeUtils'
// 顶部信息区组件，展示封面、标题、作者、分类、标签、时间、阅读数、点赞数
const { article, commentsCount } = defineProps({
  article: { type: Object, required: true },
  commentsCount: { type: Number, default: 0 },
})
function estimateReadTime(content) {
  // 简单估算：按每分钟 300 字
  if (!content) return ''
  const text = typeof content === 'string' ? content : content.text || ''
  const len = text.length
  const min = Math.max(1, Math.round(len / 300))
  return `${min}分钟`
}
</script>

<style lang="scss" scoped>
.article-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  &.with-cover {
    .cover-wrapper {
      width: 100%;
      max-height: 320px;
      overflow: hidden;
      border-radius: 12px;
      margin-bottom: 24px;
      .cover-image {
        width: 100%;
        height: 320px;
        object-fit: cover;
        display: block;
        border-radius: 12px;
      }
    }
  }
  .header-content {
    width: 100%;
    text-align: center;
    .title {
      font-size: 2.2rem;
      font-weight: 700;
      margin-bottom: 16px;
      color: var(--text-color);
      word-break: break-word;
    }
    .meta {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      gap: 18px;
      font-size: 15px;
      color: var(--text-color-secondary);
      margin-bottom: 12px;
      i {
        margin-right: 4px;
      }
    }
    .tags {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      gap: 10px;
      .tag {
        background: var(--primary-color);
        color: #fff;
        border-radius: 12px;
        padding: 2px 12px;
        font-size: 13px;
        display: flex;
        align-items: center;
        gap: 3px;
        i {
          font-size: 13px;
        }
      }
    }
  }
}
@media (max-width: 675px) {
  .article-header.with-cover .cover-wrapper {
    max-height: 160px;
    .cover-image {
      height: 160px;
    }
  }
  .article-header {
    .header-content {
      .title {
        font-size: 1.3rem;
      }
      .meta {
        font-size: 13px;
        gap: 10px;
      }
      .tags {
        .tag {
          font-size: 12px;
          padding: 2px 8px;
        }
      }
    }
  }
}
:root.dark .article-header .header-content .tags .tag {
  background: var(--primary-color-dark);
}
</style>
